#!/usr/bin/env node

// let fileLevel = 10;
const fs = require('fs');
const path = require('path');
let loopTest = 0;
const [, , ...args] = process.argv

/**
 * 
 * @param {*} target 目标文件夹
 * @param {*} deep 层级深度
 * @param {*} option 配置参数
 */

function showConsoleFn (returnStr, maxLength, baseLen, baseString) {
    if (!!maxLength) {
        let arrLen = maxLength + baseLen - returnStr.length
        arrLen = arrLen > 0 ? arrLen : 0
        console.log(`${returnStr}${new Array(arrLen).join(' ')}${baseString}`)
    } else {
        console.log(returnStr)
    }

}

function loadTree (target, deep, option = {}) {
    /**
     * maxLength 最大长度, 
     * baseLen 空格位数
     * baseString 对齐字符串
     * fileLevel 层级
     */
    let { maxLength = 21, baseLen = 3, baseString = '#', fileLevel = 10 } = option
    // if (loopTest == 4) { return; }
    loopTest++;
    let prev = new Array(deep).join(' |');
    // 前面| 字符串
    let dirinfo = fs.readdirSync(target);
    let files = [];
    let dirs = [];
    //保存文件或者是文件夹


    //遍历将文件或者文件夹分开存储
    // console.log('==loopTest', loopTest)
    for (let i = 0; i < dirinfo.length; i++) {
        // console.log(path.join(target,dirinfo[i]))
        let state = fs.statSync(path.join(target, dirinfo[i]));
        if (state.isFile()) {
            files.push(dirinfo[i])
        } else {
            dirs.push(dirinfo[i])
        }
    }

    /**
     * 判断遍历层级; 默认最多10层
     * -1 表示遍历所有层级
     * fileLevel 根据输入展示层级
     */
    let deepScan = fileLevel == -1 ? true : deep <= fileLevel;
    if (deepScan) {
        // 文件夹操作
        for (let i = 0; i < dirs.length; i++) {
            let returnStr = `${prev} ├─ ${dirs[i]}`
            showConsoleFn(returnStr, maxLength, baseLen, baseString)
            // if (maxLength) {
            //     let arrLen = maxLength + baseLen - returnStr.length
            //     arrLen = arrLen > 0 ? arrLen : 0
            //     console.log(returnStr + new Array(arrLen).join(" ") + baseString)
            // }
            // 递归
            let nextPath = path.join(target, dirs[i])
            let nextdeep = deep + 1;
            // 下一级的 文件目录 以及层级
            loadTree(nextPath, nextdeep, option)
            // 递归调用

        }
        // 文件操作
        for (let i = files.length - 1; i >= 0; i--) {
            let returnStr = ""
            if (i === 0) {
                returnStr = `${prev} └─ ${files[i]}`
                showConsoleFn(returnStr, maxLength, baseLen, baseString)
                // console.log(`${prev} └─ ${files[i]}`)
                // console.log(returnStr + new Array(maxLength + baseLen - returnStr.length).join(" ") + baseString)
            } else {
                returnStr = `${prev} ├─ ${files[i]}`
                showConsoleFn(returnStr, maxLength, baseLen, baseString)
                // console.log(`${prev} ├─ ${files[i]}`)
                // console.log(returnStr + new Array(maxLength + baseLen - returnStr.length).join(" ") + baseString)
            }

        }
    }

}
module.exports = loadTree;